package com.heima.admin.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.heima.admin.entity.AdChannel;
import com.heima.admin.service.AdChannelService;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.model.admin.dtos.ChannelDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "频道管理",description = "频道管理API")
@RequestMapping("/api/v1/channel")
public class ChannelController {
    @Autowired
    private AdChannelService channelService;

    //  频道管理--分页查询频道信息
    @PostMapping("/list")
    @ApiOperation(value = "根据名称分页查询频道列表")
    @ApiImplicitParam(name = "dto", value = "频道请求对象", required = true, dataType = "ChannelDto")
    public PageResult<ChannelDto> findByPage(@RequestBody ChannelDto dto){
        return channelService.findByPage(dto);
    }


    /**
     * 保存频道信息
     * @param channelDto
     * @return
     */
    @ApiOperation(value = "新增频道信息")
    @PostMapping("/save")
    public ResponseResult save(@RequestBody ChannelDto channelDto){
//        参数判断
        if(channelDto == null || StringUtils.isBlank(channelDto.getName())){
            return ResponseResult.error(AppHttpCodeEnum.PARAM_REQUIRE);
        }
//        对象转换
        AdChannel adChannel = BeanHelper.copyProperties(channelDto, AdChannel.class);
        adChannel.setCreatedTime(new Date());
        boolean b = channelService.save(adChannel);
        if(!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok();
    }

    /**
     * 修改频道
     * @param channelDto
     * @return
     */
    @ApiOperation(value = "更新频道信息")
    @PutMapping("/update")
    public ResponseResult update(@RequestBody ChannelDto channelDto){
//        参数判断
        if(channelDto == null || channelDto.getId() == null){
            return ResponseResult.error(AppHttpCodeEnum.PARAM_REQUIRE);
        }
//        对象转换
        AdChannel adChannel = BeanHelper.copyProperties(channelDto, AdChannel.class);
        boolean b = channelService.updateById(adChannel);
        if(!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.ok();
    }

    @ApiOperation(value = "删除频道信息")
    @DeleteMapping("/del/{id}")
    public ResponseResult deleteById(@PathVariable("id") Long id){
        channelService.delete(id);
        return ResponseResult.ok();
    }

    /**
     * 查询所有的频道信息
     * @return
     */
    @GetMapping("/channels")
    @ApiOperation(value = "查询所有的频道信息")
    public ResponseResult findAllChannel(){
        List<AdChannel> adChannelList = channelService.list();
        if (CollectionUtils.isEmpty(adChannelList)) {
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST,"查询频道信息异常！");
        }
        return ResponseResult.ok(adChannelList);
    }

}
